package cn.edu.tsinghua.weibo.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import cn.edu.tsinghua.weibo.entity.User;
import cn.edu.tsinghua.weibo.util.DB;

@SuppressWarnings("serial")
public class Settings extends HttpServlet {

	private final static String SQL_AVATAR = "select avatar from user where id=?";
	
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
//System.out.println("settings servlet ...");
		String type = request.getParameter("type");
		HttpSession session = request.getSession();
		int userid = ((User)session.getAttribute("user")).getId();
		Connection connection = DB.getConnection();
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		if(type.equals("avatar")) {
			try {
				preparedStatement = connection.prepareStatement(SQL_AVATAR);
				preparedStatement.setInt(1, userid);
				resultSet = preparedStatement.executeQuery();
				if(resultSet.next()) {
					String avatar = resultSet.getString("avatar");
					request.setAttribute("avatar", avatar);
					request.getRequestDispatcher("avatar.jsp").forward(request, response);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else {
			//...
		}
//System.out.println("close ...");
		DB.close(resultSet, preparedStatement, connection);
		
	}

}
